/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.model;

import java.util.HashSet;
import java.util.Vector;

import be.gnx.ganesa.application.command.CommandStack;
import be.gnx.ganesa.application.event.ProjectCreatedEvent;
import be.gnx.ganesa.application.event.ProjectCreatedListener;

public class Application {

  private HashSet<Project> projects = new HashSet<Project>();

  private Project selectedProject;

  private static Application instance;

  private CommandStack commandStack = new CommandStack();

  private Application() {
    instance = this;
  }

  public static Application getInstance() {
    if (instance == null) {
      instance = new Application();
    }
    return instance;
  }

  public Project getSelectedProject() {
    return selectedProject;
  }

  public void setSelectedProject(Project project) {
    this.selectedProject = project;
  }

  public Project[] getProjects() {
    return projects.toArray(new Project[0]);
  }

  public Project newProject() {
    Project project = new Project();
    projects.add(project);
    selectedProject = project;
    for (ProjectCreatedListener projectCreatedListener : projectCreatedListeners) {
      projectCreatedListener.projectCreatedEventhappened(new ProjectCreatedEvent(project));
    }
    return project;
  }

  public CommandStack getCommandStack() {
    return commandStack;
  }

  /* event listeners */
  private Vector<ProjectCreatedListener> projectCreatedListeners = new Vector<ProjectCreatedListener>();

  public void addProjectCreatedListener(ProjectCreatedListener projectCreatedListener) {
    if (projectCreatedListener == null) {
      throw new NullPointerException();
    } else {
      projectCreatedListeners.add(projectCreatedListener);
    }
  }

  public void removeProjectCreatedListener(ProjectCreatedListener projectCreatedListener) {
    if (projectCreatedListener == null) {
      throw new NullPointerException();
    } else {
      projectCreatedListeners.remove(projectCreatedListener);
    }
  }

}
